#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.Margins;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.BoxComponent;
import com.gwtext.client.widgets.Container;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.event.ContainerListenerAdapter;
import com.gwtext.client.widgets.layout.AccordionLayout;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.CardLayout;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Application implements EntryPoint {

	private static Application desktop;
	
	public static Application getInstance() {
		return desktop;
	}
	
	private Panel mainPanel;
	
	private HTML caption = new HTML();

	private final CardLayout cardLayout = new CardLayout(true);
	
	public void showPanel2() {
		cardLayout.setActiveItem(1);
	}

	public void showPanel1() {
		cardLayout.setActiveItem(0);
	}

	private Panel createMainPanel() {

		Panel panel = new Panel();
        panel.setLayout(new FitLayout());

        Panel borderPanel = new Panel();
        borderPanel.setLayout(new BorderLayout());

        //
        // NORTH PANEL
        //     
        Panel northPanel = new Panel();
        northPanel.setBorder(false);
        northPanel.setLayout(new ColumnLayout());
        northPanel.setFrame(true);
        northPanel.setHeight(50);
        BoxComponent logo = new BoxComponent();
        
        logo.setHeight(30);
        logo.setEl( caption.getElement() );
        
        northPanel.add(logo, new ColumnLayoutData(.4));
        
        northPanel.add(new Panel(), new ColumnLayoutData(.6));  
        
        Panel rightPanel = new Panel();
        rightPanel.setWidth( 70 );

        final Hyperlink logOff = new Hyperlink( "Log Off", "x");
        logOff.addClickListener( new ClickListener() {

			public void onClick(Widget sender) {
				logOff();
				
			}
        	
        });
        rightPanel.add( logOff );
        northPanel.add( rightPanel );  
        

        borderPanel.add(northPanel, new BorderLayoutData(RegionPosition.NORTH));

        //
        // SOUTH PANEL
        //        
        BorderLayoutData southData = new BorderLayoutData(RegionPosition.SOUTH);
        southData.setMinSize(100);
        southData.setMaxSize(200);
        southData.setMargins(new Margins(0, 0, 0, 0));
        southData.setSplit(true);

        Panel southPanel = createMessagePanel();
        
        borderPanel.add(southPanel, southData);
        
        //
        // WEST PANEL
        //        
        BorderLayoutData westData = new BorderLayoutData(RegionPosition.WEST);
        westData.setSplit(true);
        westData.setMinSize(175);
        westData.setMaxSize(400);
        westData.setMargins(new Margins(0, 5, 0, 0));

        final boolean animate = true;
        final Panel accordionPanel = new Panel();  
        accordionPanel.setWidth(200);
        accordionPanel.setCollapsible(true);
        accordionPanel.setTitle("Menu");	
        accordionPanel.setLayout(new AccordionLayout(animate));  
        
        
        Panel accordion1 = new Panel();
        accordion1.setTitle( "panel1" );
        accordion1.setId("accordion1");

        Panel accordion2 = new Panel();
        accordion2.setTitle( "panel2" );
        accordion2.setId("accordion2");
        
        accordionPanel.add( accordion1 );
        accordionPanel.add( accordion2 );

        
        accordionPanel.addListener( new ContainerListenerAdapter() {

			@Override
			public void onAfterLayout(Container self) {
				final AccordionLayout layout = (AccordionLayout) accordionPanel.getLayout(); 
						
				GWT.log("onAfterLayout " + layout.getActiveItem().getId(), null);
				
				if( "accordion1".equals(layout.getActiveItem().getId()) ) {
					showPanel1();
				} else if( "accordion2".equals(layout.getActiveItem().getId()) ) {
					showPanel2();
				} 			
			}
     
        });
        
        borderPanel.add(accordionPanel, westData);
        
        //
        // CENTER PANEL
        //
        BorderLayoutData centerLayoutData = new BorderLayoutData(RegionPosition.CENTER);
        centerLayoutData.setMargins(new Margins(5, 0, 5, 5));

        final Panel panel1 = createPanel1();     
        final Panel panel2 = createPanel2();

        final Panel deckPanel = new Panel();
        deckPanel.setLayout(cardLayout);
        
        deckPanel.add( panel1 );
        deckPanel.add( panel2 );
        deckPanel.setActiveItem(0);
        borderPanel.add(deckPanel, centerLayoutData);

        panel.add( borderPanel );

        return panel;
	}
	
	private Panel createPanel1() {
		Panel panel = new Panel();
		panel.setTitle("Pannello 1");
        panel.setLayout(new FitLayout());
        panel.setBorder(false);
        panel.setBodyBorder(false);
		return panel;
	}

	private Panel createPanel2() {
		Panel panel = new Panel();
		panel.setTitle("Pannello 2");
		panel.setFrame(true);
		panel.setLayout( new FitLayout() );
		return panel;
	}
	
	/**
	 * 
	 * @return
	 */
	private Panel createMessagePanel() {
        Panel panel = new Panel();
        //panel.setTitle("South");
        panel.setHeight(100);
        panel.setCollapsible(true);
        return panel;
	}

	/**
	 * 
	 */
	protected void logOff() {

	}

	/**
	 * 
	 */
	public void logIn() {  
	}
  
   /**
	* 
	* This is the entry point method.
	* 
	*/
	public void onModuleLoad() {
	    
		desktop = this;
	    
	    mainPanel = createMainPanel( );
	    
	    new Viewport( mainPanel );
	    
	    logIn();
	      
	}
}
